<template>
  <div class="login_container">
    <div class="login_box">
      <!--头像区域-->
      <div class="avatar_box">
        <img src="../assets/logo.png" alt="">
      </div>
      <!--登录表单区域-->
      <el-form ref="loginFormRef" :model="loginForm" :rules="loginFormRules" label-width="90px" class="login_form">
        <!--用户名-->
        <el-form-item label="用户id" prop="userID">
          <el-input v-model="loginForm.userID"
                    placeholder="请输入内容"
                    prefix-icon="el-icon-user-solid">
          </el-input>
        </el-form-item>
        <!--密码-->
        <el-form-item label="密码" prop="password">
          <el-input v-model="loginForm.password" type="password"
                    placeholder="请输入内容"
                    prefix-icon="el-icon-lock">
          </el-input>
        </el-form-item>
        <!--按钮-->
        <el-form-item class="btns">
          <el-button type="primary" @click="login">登录</el-button>
          <el-button type="info" @click="resetLoginForm">重置</el-button>
        </el-form-item>

      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  name: 'Login',
  data () {
    return {
      //这是登录表单的数据绑定对象
      loginForm: {
        userID: '',
        password: ''
      },
      //这是表单的验证规则对象
      loginFormRules: {
        userID: [
          { required: true, message: '请输入用户id', trigger: 'blur' },
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
        ]
      }
    }
  },
  methods: {
    resetLoginForm () {
      this.$refs.loginFormRef.resetFields()
    },
    login () {
      this.$refs.loginFormRef.validate(async valid => {
        if (!valid) return
        const { data: res } = await this.$http.post('login', this.loginForm)
        if (res.code == 10410) return this.$message.error('用户已离职！')
        if (res.code == 10404) return this.$message.error('用户名或密码错误！')
        if(res.code == 10200){
          this.$message.success('登录成功')
          window.sessionStorage.clear()
          console.log(res)
          window.sessionStorage.setItem('role_id', res.data.roleID)
          window.sessionStorage.setItem('token', res.data.accessToken)
          window.sessionStorage.setItem('userID', res.data.userID)
          window.sessionStorage.setItem('userName', res.data.username)
          //登录成功跳转页面
          if(res.data.roleID==100){
            await this.$router.push('/bosshome')
          }
          if(res.data.roleID==111){
            await this.$router.push('/cashierhome')
          }

        }

      })
    }
  },
}
</script>

<style lang="less" scoped>
.login_container {
  background-color: #91C7FF;
  height: 100%;
}

.login_box {
  width: 450px;
  height: 300px;
  background-color: #fff;
  border-radius: 3px;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%); /*把盒子放到正中间*/

  .avatar_box {
    height: 130px;
    width: 130px;
    border: 1px solid #333333;
    border-radius: 50%;
    padding: 5px;
    background-color: #fff;
    position: absolute;
    left: 50%;
    transform: translate(-50%, -50%);

    img {
      width: 100%;
      height: 100%;
      border-radius: 50%;
    }
  }
}

.login_form {
  position: absolute;
  bottom: 5px;
  left: 30px;
  width: 75%;
}

.btns {
  display: flex;
  justify-content: flex-end;
}
</style>
